package com.cvit.service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cvit.dao.SchoolMajorMapper;
import com.cvit.model.SchoolMajor;
import com.cvit.utils.ExcelUtil;
import com.cvit.utils.Utils;

@Service
public class SchoolMajorService {

	@Autowired
	SchoolMajorMapper schoolMajorMapper;

	/**
	 * 查询所有信息
	 * */
	public List<SchoolMajor> getAll() {
		List<SchoolMajor> schoolMajor = schoolMajorMapper.selectByExample(null);
		return schoolMajor;
	}
	
	/**
	 * 根据Id查询 （boolean）
	 * */
	public boolean getSchoolMajorById(String id){
		SchoolMajor schoolMajor = schoolMajorMapper.selectByPrimaryKey(id);
			return Utils.isNull(schoolMajor);
	}
	
	/**
	 * 查询所有学校
	 * */
	public List<SchoolMajor> selschool(){
		List<SchoolMajor> schoolMajor = schoolMajorMapper.selschool();
		return schoolMajor;
	}
	
	/**
	 * 查询所有专业
	 * */
	public List<SchoolMajor> selmajor(String school){
		if (school==""||school==null) {
			return null;
		}else{
			List<SchoolMajor> schoolMajor = schoolMajorMapper.selmajor(school);
			return schoolMajor;
		}
	}
	
	/**
	 * 导入Excel
	 * */
	public void importExcelInfo(InputStream in, MultipartFile file) throws Exception{
	    List<List<Object>> listob = ExcelUtil.getBankListByExcel(in,file.getOriginalFilename()); 
	    for (int i = 0; i < listob.size(); i++) {
	        List<Object> ob = listob.get(i);
	        SchoolMajor schoolMajor = new SchoolMajor();
	        schoolMajor.setId(Utils.getsUUID());
	        schoolMajor.setSchool(String.valueOf(ob.get(0)));
	        schoolMajor.setSchoolNum(String.valueOf(ob.get(1)));
	        schoolMajor.setMajor(String.valueOf(ob.get(2)));
	        schoolMajor.setMajorNum(String.valueOf(ob.get(3)));
	        schoolMajor.setDirection(String.valueOf(ob.get(4)));
	        schoolMajor.setScience(2);
	        schoolMajor.setPrice(Integer.valueOf((String) ob.get(6)));
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        schoolMajor.setStartTime(sdf.parse(String.valueOf(ob.get(7))));
	        boolean achievementId = getSchoolMajorById(String.valueOf(ob.get(0)));
	        if(achievementId==false){
	        	schoolMajorMapper.deleteByPrimaryKey(schoolMajor.getId());
	        }
	        	schoolMajorMapper.insert(schoolMajor);
	    }  
	}
	
}
